/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

/**
 * 
 * would consider http://lloydforge.org/projects/yajl/ in the future 
 * as the claim is that it can handle large JSON files
 * */

#ifndef NYMRL_MESSAGE_H_
#define NYMRL_MESSAGE_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#ifndef TRUE
#define TRUE (1==1)
#endif

#ifndef FALSE
#define FALSE (0==2)
#endif

#define NYMRL_URL char *
#define NYMRL_URL_LIST char **
#define NYMRL_NODE_AND_URL_LIST char **
#define NYMRL_ID unsigned long
#define NYMRL_ACTION int 
////////////////////////
//ACTIONS /message ids
// TBD by implementation
#define NYMRL_ACTION_NULL 0
#define NYMRL_ROUTER_VERSION 1

/**
 * this defines request string 
 * http://www.allegro.cc/forums/thread/590278
 * //nid,action/function,rel,(auth),forward_[one|any|all|any_then_all|one_then_all],dat
 * 
 * new, dat base 64 encoded
 * 
 * */
#define NYMRL_HEADER "nid=%lu&rel=%s&time=%d&msid=%s&rmid=%s&chck=%lu&enc=%d&fwd=%d&auth=%s&dat=%s&act=%s&endpadding"
#define NYMRL_HEADER_SCAN "nid=%ld&rel=%[^&]&time=%d&msid=%[^&]&rmid=%[^&]&chck=%ld&enc=%1d&fwd=%d&auth=%[^&]&dat=%[^&]&act=%[^&]&endpadding"
//
#define NYMRL_HEADER_STR_LEN 32  
#define NYMRL_HEADER_STR_SIZE (NYMRL_HEADER_STR_LEN * sizeof(char))

#define NYMRL_URL_CURL_HTTP_AGENT_NAME "NYMRL - cURL .1"

//see also nymrl_first | second| third
enum nymrl_fwd { NONE, ALL, ANY, ONE, SEND_BEFORE=10,SEND_AFTER=20, RESPOND_BEFORE=100, RESPOND_AFTER=200  };

/**
 * move around data for a string
 * machin id // session 
 * */
typedef struct {
	NYMRL_ID nid;
	char rel[NYMRL_HEADER_STR_LEN];
	char msid[NYMRL_HEADER_STR_LEN];
	char rmid[NYMRL_HEADER_STR_LEN];
	char auth[NYMRL_HEADER_STR_LEN];
	char act[NYMRL_HEADER_STR_LEN];
	unsigned long chck;
	int enc;
	int time;
	enum nymrl_fwd fwd; 
	char * dat ; 
} nyml_header_struct;

//http://curl.haxx.se/libcurl/c/example.html
//http://curl.haxx.se/lxr/source/docs/examples/getinmemory.c
struct nymrl_curl_struct {
   char *memory;
   size_t size;
};

inline nyml_header_struct * nymrl_get_header_from_string(char * request_string, int _free);

inline char * nymrl_get_string_from_header( nyml_header_struct * this, int _free);
inline void nymrl_free_header_struct( nyml_header_struct * this);

inline void * nymrl_new_header(
		NYMRL_ID nid, 
		char * rel,
		int time,
		char * msid,
		char * rmid,
		unsigned long chck,
		int enc,
		enum nymrl_fwd fwd,
		char * auth,
		char * dat,
		char * act,
		const char * string_or_struct,
		int _free);

inline char * nymrl_encrypt(char * string);
inline char * nymrl_decrypt(char * string);


static inline size_t  nymrl_curl_memory_callback(void *ptr, size_t size, size_t nmemb, void *data);
static inline void *nymrl_curl_realloc(void *ptr, size_t size);
inline char * nymrl_curl_get(char * url, char * cookies);
inline int nymrl_curl_blind_forward(char * url, char * post_data, char * cookies);
inline char * nymrl_curl_post(char * url, char * post_data, char * cookies);
inline int nymrl_valid_string(char * string);

#endif /*NYMRL_MESSAGE_H_*/


